package cn.bdqn.controller.course;

import cn.bdqn.dto.LoginUser;
import cn.bdqn.pojo.course.CourseCatalog;
import cn.bdqn.pojo.course.CourseVideo;
import cn.bdqn.service.course.CourseCatalogService;
import cn.bdqn.service.course.CourseVideoService;
import cn.bdqn.utils.Json;
import cn.bdqn.vo.course.CourseCatalogVideoVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.FileSystemUtils;
import org.springframework.web.bind.annotation.*;

import java.io.File;
import java.util.List;
import java.util.Objects;

@RestController
@RequestMapping("/api")
public class CourseCatalogController {

    @Autowired
    private CourseCatalogService courseCatalogService;

    @Autowired
    private CourseVideoService courseVideoService;

    /**
     * 根据课程id、目录名称、视频名称查询目录信息及其下视频信息
     * @param courseId  课程id
     * @param catalogName   目录名称
     * @param videoName 视频名称
     * @return  返回json对象
     */
    @PreAuthorize("hasAuthority('course:management')")
    @GetMapping("/selectCourseCatalogVideoVO")
    public Object selectCourseCatalogVideoVO(Integer courseId,String catalogName,String videoName){
        //检查课程id是否异常
        if (null != courseId && courseId <0) {
            return new Json(400,"课程id不能为空");
//            courseId = null;
        }
        if (null != catalogName && catalogName.trim().length() == 0) {
            catalogName = null;
        }
        if (null != videoName && videoName.trim().length() == 0) {
            videoName = null;
        }

        List<CourseCatalogVideoVo> courseCatalogVideoVos = courseCatalogService.selectCourseCatalogVideoVO(courseId, catalogName, videoName);

        return Json.isEmpty(courseCatalogVideoVos);
    }

    /**
     * 根据目录id查询目录信息
     * @param id    目录id
     * @return  返回目录信息对象
     */
    @GetMapping("/selectCourseCatalogById")
    public  Object selectCourseCatalogById(Integer id){
        //检查目录id是否为空
        if (null == id || id<0) {
            return new Json(400,"目录id不能为空");
        }
        CourseCatalog courseCatalog = courseCatalogService.selectCourseCatalogById(id);
        return Json.isEmpty(courseCatalog);
    }

    /**
     * 根据课程id查询其下所有目录信息
     * @param courseId  课程id
     * @return  返回目录信息集合
     */
    @GetMapping("/selectCatalogByCourseId")
    public Object selectCatalogByCourseId(Integer courseId){
        //检查课程id是否为空
        if (null == courseId || courseId < 0) {
            return new Json(400,"课程id不能为空");
        }
        List<CourseCatalog> courseCatalogs = courseCatalogService.selectCourseCatalogByCourseId(courseId);
        return Json.isEmpty(courseCatalogs);
    }

    /**
     * 添加课程目录信息
     * @param courseCatalog 添加的目录对象
     * @return  返回json对象
     */
    @PreAuthorize("hasAuthority('course:management')")
    @PostMapping("/insertCourseCatalog")
        public Object insertCourseCatalog(@RequestBody CourseCatalog courseCatalog){

        Json json =null;
        if (null == courseCatalog.getCourseId() || courseCatalog.getCourseId() < 0) {
            //检查课程id是否为空
            json = new Json(400,"课程id不能为空");
        }else if (null == courseCatalog.getCourseCatalogName()||courseCatalog.getCourseCatalogName().length() == 0){
            json = new Json(400,"课程名称不能为空");
        }

        if (null == json) {
            Integer integer = courseCatalogService.insertCourseCatalog(courseCatalog);
            if(integer<0){
                json = new Json(400,"添加失败");
            }else {
                json = new Json(200,"添加成功");
            }
        }
        return json;
    }

    /**
     * 根据目录id修改目录状态信息
     * @param catalogId 目录id
     * @param status    修改后的状态信息
     * @return  返回json对象
     */
    @PreAuthorize("hasAuthority('course:management')")
    @PutMapping("/updateCourseCatalogStatusById")
    public Object updateCourseCatalogStatusById(@RequestParam("catalogId") Integer catalogId,@RequestParam("status") Integer status){

        Json json = null;
        if(null == catalogId){
            //检查id是否为空
            json =  new Json(400,"目录id不能为空");
        }else if (null == status || status >1 || status < 0) {
            json =  new Json(400,"标签状态不能为空");
        }

        if (null == json) {
            Integer integer = courseCatalogService.updateCourseCatalogStatusById(catalogId, status);
            if(integer<0){
                json = new Json(400,"更新失败");
            }else {
                json = new Json(200,"更新成功");
            }
        }
        return json;
    }

    /**
     * 根据课程目录id修改目录信息
     * @param courseCatalog 修改后的目录信息对象
     * @return  返回json对象
     */
    @PreAuthorize("hasAuthority('course:management')")
    @PutMapping("/updateCourseCatalogById")
    public Object updateCourseCatalogById(@RequestBody CourseCatalog courseCatalog){
        Json json = null;
        if(null == courseCatalog){
            json = new Json(400,"目录信息不能为空");
        }else if(null == courseCatalog.getCourseCatalogId()){
            //检查id是否为空
            json =  new Json(400,"目录id不能为空");
        }else if (null == courseCatalog.getCourseCatalogStatus() || courseCatalog.getCourseCatalogStatus() >1 || courseCatalog.getCourseCatalogStatus() < 0) {
            json =  new Json(400,"标签状态不能为空");
        }

        if (null == json) {
            Integer integer = courseCatalogService.updateCourseCatalogById(courseCatalog);
            if(integer<0){
                json = new Json(400,"更新失败");
            }else {
                json = new Json(200,"更新成功");
            }
        }
        return json;
    }

    /**
     * 根据目录、课程id删除目录信息及其下视频信息和视频文件
     * @param catalogId 目录id
     * @param courseId  课程id
     * @param realPath  文件存放的绝对路径
     * @param videoPath 视频文件相对路径
     * @return  返回json对象
     */
    @PreAuthorize("hasAuthority('course:management')")
    @DeleteMapping("/deleteCatalogById")
    @Transactional(rollbackFor = RuntimeException.class)
    public Object deleteCatalogById(Integer catalogId, Integer courseId, @Value("${prop.realPath}") String realPath,@Value("${prop.upload-video}") String videoPath){
        Json json = null;
        //检查课程id是否为空
        if (null == catalogId || catalogId <0) {
            json =  new Json(400,"目录id不能为空");
        }else if (null == courseId || courseId< 0){
            json =  new Json(400,"课程id不能为空");
        }else{
            //删除数据库中的视频信息
            int i = courseVideoService.deleteCourseVideoByCatalogId(catalogId);
            if (i<0){
                throw new RuntimeException("课程视频删除失败");
            }else {
                //删除目录
                int j = courseCatalogService.deleteCourseCatalogById(catalogId);
                if (j<0){
                    throw new RuntimeException("课程目录删除失败");
                }else {
                    //检查该目录下是否存在视频文件
                    File file = new File(realPath + videoPath + "/" + courseId + "/" + catalogId);
                    if (file.exists()) {
                        //删除视频文件
                        boolean b = FileSystemUtils.deleteRecursively(file);
                        if (!b) {
                            throw new RuntimeException("视频文件删除失败");
                        }
                    }
                    json = new Json(200,"删除成功");
                }

            }
        }
        return json;
    }
}
